package com.example.Servlet;

import com.example.Pojo.HairMaster;
import com.example.Pojo.Member;
import com.example.dao.impl.AdminManagerImpl;
import com.example.dao.impl.UserDaoImpl;
import com.google.gson.Gson;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class AdminOpt extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        //反射
        String action = req.getParameter("action");
        try {
            Method method = this.getClass().getDeclaredMethod(action,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        System.out.println("修改信息...");
        HairMaster hm = new HairMaster();
        //Gson gson = new Gson();
        Map parameterMap = req.getParameterMap();
        try {
            BeanUtils.populate(hm,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(hm);
        if (new AdminManagerImpl().UpdateInfo(hm) >0){
            HttpSession session = req.getSession();
            List<HairMaster> list = new UserDaoImpl().findAllMaster();
            session.setAttribute("hairmaster",list);
            System.out.println("修改成功!");
        }else {
            System.out.println("修改失败!");
        }
    }
}
